﻿using DevExpress.ExpressApp.DC;
using System.Linq.Expressions;

namespace EasyXaf.EntityDesigner.ModelBuilders;

public class EntityTypeBuilder<TEntity> : IEntityTypeBuilder
{
    private readonly ITypeInfo _typeInfo;

    public EntityTypeBuilder(ITypeInfo typeInfo)
    {
        _typeInfo = typeInfo;
    }

    public EntityPropertyBuilder<TProperty> Property<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression)
    {
        var memberExpression = propertyExpression.Body as MemberExpression;
        var memberType = _typeInfo.FindMember(memberExpression.Member.Name);
        return new EntityPropertyBuilder<TProperty>(memberType);
    }

    public EntityTypeBuilder<TEntity> AddAttribute(Func<ITypeInfo, Attribute> action)
    {
        var attribute = action(_typeInfo);
        if (attribute != null)
        {
            _typeInfo.AddAttribute(attribute);
        }
        return this;
    }

    public EntityTypeBuilder<TEntity> RemoveAttribute(Func<ITypeInfo, Attribute> action)
    {
        var attribute = action(_typeInfo);
        if (attribute != null)
        {
            ((BaseInfo)_typeInfo).RemoveAttribute(attribute);
        }
        return this;
    }
}
